/*
 * @Author: your name
 * @Date: 2021-04-22 14:49:42
 * @LastEditTime: 2021-04-22 16:13:03
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \chronic-back-end\module\healthyMod\controller.go
 */
package healthMod

import (
	"chronic/module/userMod"
	"net/http"

	"github.com/gin-gonic/gin"
)

type healthDataDto struct {
	BodyWeight     string `form:"weight" json:"weight" binding:"required"`
	HeartRate      string `form:"rate" json:"rate" binding:"required"`
	PressureShrink string `form:"pressureShrink" json:"pressureShrink" binding:"required"`
	PressureExtend string `form:"pressureExtend" json:"pressureExtend" binding:"required"`
	BloodSugar     string `form:"sugar" json:"sugar" binding:"required"`
	BloodLipidsSum string `form:"lipidsSum" json:"lipidsSum" binding:"required"`
	BloodLipidsGan string `form:"lipidsGan" json:"lipidsGan" binding:"required"`
}

func postHealthData(ctx *gin.Context) {
	user := ctx.MustGet("user").(userMod.User)
	var input healthDataDto
	if err := ctx.ShouldBind(&input); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	data, err := addHealthData(user.ID, input)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
	} else {
		ctx.JSON(http.StatusOK, data)
	}
}

func getHealthDataHistory(ctx *gin.Context) {
	user := ctx.MustGet("user").(userMod.User)
	datas, err := findHealthDataHistory(user.ID)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err})
	} else {
		ctx.JSON(http.StatusOK, datas)
	}
}
